package com.cyt.work.antiepidemic.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.antiepidemic.dao.AntiepidemicPersonDao;
import com.cyt.work.antiepidemic.model.po.AntiepidemicPerson;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicPersonExportVo;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicPersonVo;
import com.cyt.work.antiepidemic.service.AntiepidemicPersonService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * e门通-社区住户信息Service层实现类
 *
 * @author rong yuan
 * @date 2020-2-12 2:07:52
 */
@Service("antiepidemicPersonService")
public class AntiepidemicPersonServiceImpl extends BaseServiceImpl<AntiepidemicPerson> implements AntiepidemicPersonService {

    @Resource
    private AntiepidemicPersonDao antiepidemicPersonDao;

    @Autowired
    public void setBaseDao(AntiepidemicPersonDao antiepidemicPersonDao) {
        this.baseDao = antiepidemicPersonDao;
    }

    /**
     * 查询对象集合
     *
     * @param antiepidemicPersonVo 对象
     * @return List 对象集合
     * @author rong yuan
     * @date 2020-2-12 2:07:52
     */
    @Override
    public List<AntiepidemicPerson> listExample(AntiepidemicPersonVo antiepidemicPersonVo) {
        Example example = new Example(AntiepidemicPerson.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        if (StringUtils.isNotBlank(antiepidemicPersonVo.getApplyId())) {
            criteria.andEqualTo("applyId", antiepidemicPersonVo.getApplyId());
        }
        if (StringUtils.isNotBlank(antiepidemicPersonVo.getName())) {
            criteria.andLike("name", "%" + antiepidemicPersonVo.getName() + "%");
        }
        if (StringUtils.isNotBlank(antiepidemicPersonVo.getMobile())) {
            criteria.andLike("mobile", "%" + antiepidemicPersonVo.getMobile() + "%");
        }
        if (StringUtils.isNotBlank(antiepidemicPersonVo.getIdCard())) {
            criteria.andLike("idCard", "%" + antiepidemicPersonVo.getIdCard() + "%");
        }
        if (StringUtils.isNotBlank(antiepidemicPersonVo.getBuildingId())) {
            criteria.andEqualTo("buildingId", antiepidemicPersonVo.getBuildingId());
        }
        if (StringUtils.isNotBlank(antiepidemicPersonVo.getBuildingRoom())) {
            criteria.andEqualTo("buildingRoom", antiepidemicPersonVo.getBuildingRoom());
        }
        if (StringUtils.isNotBlank(antiepidemicPersonVo.getReturnStatus())) {
            criteria.andEqualTo("returnStatus", antiepidemicPersonVo.getReturnStatus());
        }
        if (StringUtils.isNotBlank(antiepidemicPersonVo.getEpidemicArea())) {
            criteria.andEqualTo("epidemicArea", antiepidemicPersonVo.getEpidemicArea());
        }
        if (StringUtils.isNotBlank(antiepidemicPersonVo.getCertificateStatus())) {
            criteria.andEqualTo("certificateStatus", antiepidemicPersonVo.getCertificateStatus());
        }
        return antiepidemicPersonDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param antiepidemicPersonVo 接受参数对象
     * @param pageNum              分页数
     * @param pageSize             每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:07:52
     */
    @Override
    public ReturnResult page(AntiepidemicPersonVo antiepidemicPersonVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<AntiepidemicPerson> list = antiepidemicPersonDao.selectByCondition(antiepidemicPersonVo);
        PageInfo<AntiepidemicPerson> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param antiepidemicPersonVo 参数接受对象
     * @param sysUser              当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:07:52
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(AntiepidemicPersonVo antiepidemicPersonVo, SysUser sysUser) {
        if (checkPerson(antiepidemicPersonVo.getId(), antiepidemicPersonVo.getApplyId(), antiepidemicPersonVo.getMemberId(), antiepidemicPersonVo.getBuildingId(), antiepidemicPersonVo.getBuildingRoom())) {
            return new ReturnResult(ErrorCode.CODE_1, "不支持重复注册");
        }
        if (StringUtils.isBlank(antiepidemicPersonVo.getId())) {
            AntiepidemicPerson antiepidemicPerson = new AntiepidemicPerson(sysUser);
            antiepidemicPerson.setApplyId(antiepidemicPersonVo.getApplyId());
            antiepidemicPerson.setMemberId(antiepidemicPersonVo.getMemberId());
            antiepidemicPerson.setName(antiepidemicPersonVo.getName());
            antiepidemicPerson.setSexType(antiepidemicPersonVo.getSexType());
            antiepidemicPerson.setMobile(antiepidemicPersonVo.getMobile());
            antiepidemicPerson.setIdCard(antiepidemicPersonVo.getIdCard());
            antiepidemicPerson.setBuildingId(antiepidemicPersonVo.getBuildingId());
            antiepidemicPerson.setBuildingRoom(antiepidemicPersonVo.getBuildingRoom());
            antiepidemicPerson.setReturnStatus(antiepidemicPersonVo.getReturnStatus());
            if (StringUtils.isNotBlank(antiepidemicPersonVo.getReturnTime())) {
                antiepidemicPerson.setReturnTime(DateUtil.parseDateTime(antiepidemicPersonVo.getReturnTime() + " 00:00:00"));
            }
            antiepidemicPerson.setReturnArea(antiepidemicPersonVo.getReturnArea());
            antiepidemicPerson.setEpidemicArea(antiepidemicPersonVo.getEpidemicArea());
            antiepidemicPerson.setCertificateObjectId(antiepidemicPersonVo.getCertificateObjectId());
            antiepidemicPerson.setCertificateStatus(antiepidemicPersonVo.getCertificateStatus());
            antiepidemicPerson.setCertificateTime(antiepidemicPersonVo.getCertificateTime());
            antiepidemicPerson.setVerifyStatus(antiepidemicPersonVo.getVerifyStatus());
            antiepidemicPerson.setCarNumber(antiepidemicPersonVo.getCarNumber());
            antiepidemicPersonDao.insert(antiepidemicPerson);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            AntiepidemicPerson antiepidemicPerson = antiepidemicPersonDao.selectByPrimaryKey(antiepidemicPersonVo.getId());
            antiepidemicPerson.setApplyId(antiepidemicPersonVo.getApplyId());
            antiepidemicPerson.setMemberId(antiepidemicPersonVo.getMemberId());
            antiepidemicPerson.setName(antiepidemicPersonVo.getName());
            antiepidemicPerson.setSexType(antiepidemicPersonVo.getSexType());
            antiepidemicPerson.setMobile(antiepidemicPersonVo.getMobile());
            antiepidemicPerson.setIdCard(antiepidemicPersonVo.getIdCard());
            antiepidemicPerson.setBuildingId(antiepidemicPersonVo.getBuildingId());
            antiepidemicPerson.setBuildingRoom(antiepidemicPersonVo.getBuildingRoom());
            antiepidemicPerson.setReturnStatus(antiepidemicPersonVo.getReturnStatus());
            if (StringUtils.isNotBlank(antiepidemicPersonVo.getReturnTime())) {
                antiepidemicPerson.setReturnTime(DateUtil.parseDateTime(antiepidemicPersonVo.getReturnTime() + " 00:00:00"));
            }
            antiepidemicPerson.setReturnArea(antiepidemicPersonVo.getReturnArea());
            antiepidemicPerson.setEpidemicArea(antiepidemicPersonVo.getEpidemicArea());
            antiepidemicPerson.setCertificateObjectId(antiepidemicPersonVo.getCertificateObjectId());
            antiepidemicPerson.setCertificateStatus(antiepidemicPersonVo.getCertificateStatus());
            antiepidemicPerson.setCertificateTime(antiepidemicPersonVo.getCertificateTime());
            antiepidemicPerson.setVerifyStatus(antiepidemicPersonVo.getVerifyStatus());
            antiepidemicPerson.setCarNumber(antiepidemicPersonVo.getCarNumber());
            antiepidemicPerson.setUpdateTime(new Date());
            antiepidemicPerson.setUpdateUser(sysUser.getId());
            antiepidemicPersonDao.updateByPrimaryKey(antiepidemicPerson);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    private boolean checkPerson(String id, String applyId, String memberId, String buildingId, String buildingRoom) {
        AntiepidemicPerson antiepidemicPerson = new AntiepidemicPerson();
        antiepidemicPerson.setApplyId(applyId);
        antiepidemicPerson.setMemberId(memberId);
        antiepidemicPerson.setBuildingId(buildingId);
        antiepidemicPerson.setBuildingRoom(buildingRoom);
        antiepidemicPerson = antiepidemicPersonDao.selectOne(antiepidemicPerson);
        if (StringUtils.isBlank(id)) {
            return antiepidemicPerson != null;
        } else {
            return antiepidemicPerson != null && !antiepidemicPerson.getId().equals(id);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param antiepidemicPersonVo 参数接受对象
     * @param sysUser              当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:07:52
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(AntiepidemicPersonVo antiepidemicPersonVo, SysUser sysUser) {
        //要修改的字段
        AntiepidemicPerson antiepidemicPerson = new AntiepidemicPerson();
        antiepidemicPerson.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        antiepidemicPerson.setUpdateUser(sysUser.getId());
        antiepidemicPerson.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(AntiepidemicPerson.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(antiepidemicPersonVo.getId())) {
            criteria.andIn("id", Arrays.asList(antiepidemicPersonVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        antiepidemicPersonDao.updateByExampleSelective(antiepidemicPerson, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param antiepidemicPersonVo 参数接受对象
     * @param sysUser              当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:07:52
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(AntiepidemicPersonVo antiepidemicPersonVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(AntiepidemicPerson.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(antiepidemicPersonVo.getId())) {
            criteria.andIn("id", Arrays.asList(antiepidemicPersonVo.getId().split(",")));
        }
        antiepidemicPersonDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    @Override
    public List<AntiepidemicPersonExportVo> selectExportList(AntiepidemicPersonVo antiepidemicPersonVo) {
        return antiepidemicPersonDao.selectExportList(antiepidemicPersonVo);
    }

    @Override
    public ReturnResult check(String ids, String verifyStatus, SysUser sysUser) {
        if (StringUtils.isEmpty(ids) || StringUtils.isEmpty(verifyStatus)) {
            return new ReturnResult(ErrorCode.CODE_1, "请求参数不能为空");
        }
        //要修改的字段
        AntiepidemicPerson antiepidemicPerson = new AntiepidemicPerson();
        antiepidemicPerson.setVerifyStatus(verifyStatus);
        antiepidemicPerson.setUpdateUser(sysUser.getId());
        antiepidemicPerson.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(AntiepidemicPerson.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(ids)) {
            criteria.andIn("id", Arrays.asList(ids.split(",")));
        }
        antiepidemicPersonDao.updateByExampleSelective(antiepidemicPerson, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}